package com.lei.zktwo.mvp.presenter;

import android.util.Log;

import com.lei.mvptwozk.mvp.presenter.BasePresenter;
import com.lei.mvptwozk.mvp.view.IView;
import com.lei.mvptwozk.retrofit.BaseObservable;
import com.lei.mvptwozk.retrofit.BaseObserver;
import com.lei.zktwo.bean.AppkepBean;
import com.lei.zktwo.bean.LoginBean;
import com.lei.zktwo.bean.RegisterBean;
import com.lei.zktwo.mvp.contract.LoginContract;

import java.util.HashMap;

public class LoginPresenter extends BasePresenter<LoginContract.Model, IView> implements LoginContract.Presenter {
    private LoginContract.AppKeyView appKeyView;
    private LoginContract.LoginView loginView;
    private LoginContract.RegisterView registerView;
    public LoginPresenter(LoginContract.Model mmodel, LoginContract.AppKeyView appKeyView) {
        super(mmodel, appKeyView);
        this.appKeyView = (LoginContract.AppKeyView) mview;
    }
    public LoginPresenter(LoginContract.Model mmodel, LoginContract.LoginView loginView) {
        super(mmodel, loginView);
        this.loginView = (LoginContract.LoginView) mview;
    }
    public LoginPresenter(LoginContract.Model mmodel, LoginContract.RegisterView registerView) {
        super(mmodel, registerView);
        this.registerView = (LoginContract.RegisterView) mview;
    }

    @Override
    public void Appkey() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name",appKeyView.getname());
        map.put("passwd",appKeyView.getpasswd());
        map.put("email",appKeyView.getemail());
        new BaseObservable().doit(mmodel.appkey(map),new BaseObserver<AppkepBean>(){
            @Override
            public void onNext(AppkepBean appkepBean) {
                super.onNext(appkepBean);
                if (appkepBean.getResult()!=null){
                    String apikey = appkepBean.getResult().getApikey();
                    appKeyView.gomain(apikey);
                }else {
                    String message = appkepBean.getMessage();
                    Log.e("ljl", "onNext: "+message);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    @Override
    public void login() {
        HashMap<String, String> map = new HashMap<>();
        map.put("apikey",loginView.getkey());
        map.put("name",loginView.getname());
        map.put("passwd",loginView.getpasswd());
        new BaseObservable().doit(mmodel.login(map),new BaseObserver<LoginBean>(){
            @Override
            public void onNext(LoginBean loginBean) {
                super.onNext(loginBean);
                if (loginBean.getResult()!=null){
                    String message = loginBean.getMessage();
                    Log.e("ljl", "onNext: "+message);
                    loginView.gomain(message);
                }else {
                    String message = loginBean.getMessage();
                    Log.e("ljl", "onNext: "+message);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    @Override
    public void register() {
        HashMap<String, String> map = new HashMap<>();
        map.put("apikey",registerView.getkey());
        map.put("name",registerView.getname());
        map.put("passwd",registerView.getpasswd());
        new BaseObservable().doit(mmodel.register(map),new BaseObserver<RegisterBean>(){
            @Override
            public void onNext(RegisterBean registerBean) {
                super.onNext(registerBean);
                Log.e("ljl", "onNext: "+registerBean.toString() );
                if (registerBean.getResult()!=null){
                    String message = registerBean.getMessage();
                    Log.e("ljl", "onNext: "+message);
                    registerView.gomain(message);
                }else {
                    String message = registerBean.getMessage();
                    Log.e("ljl", "onNext: "+message);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    @Override
    public void destroy() {

    }
}
